package com.hr.leetcode.广度优先搜索和深度优先搜索;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 周恒哲
 * @date 2020/05/27
 * 递归+剪枝
 **/
public class _22_括号生成 {
    public List<String> generateParenthesis(int n) {
        ArrayList<String> res = new ArrayList<>();
        generateOne("",res,n,n);
        return res;
    }

    public void generateOne(String sub, List<String> res, int left, int right) {
        if (left == 0 && right == 0) {
            res.add(sub);
            return;
        }
        if (left > 0) {
            generateOne(sub + "(", res, left - 1, right);
        }
        if (right > left) {
            generateOne(sub + ")", res, left, right - 1);
        }
    }
}
